package io.rong.imkit.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import io.rong.imkit.R
import io.rong.imkit.entity.ImagesBean
import io.rong.imkit.feature.recallEdit.RecallSendCountDownCallBack
import io.rong.imkit.feature.recallEdit.RecallSendManager
import io.rong.imkit.picture.tools.DateUtils
import io.rong.imkit.widget.adapter.ViewHolder
import java.lang.ref.WeakReference

class PrivateAlbumsAdapter(
    var context: Context,
    var mUrls: ArrayList<ImagesBean>,
    var listener: OnCallBack
) :
    RecyclerView.Adapter<ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.item_private_albums, parent, false)
        return RecallEditViewHolder(context,view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val url = mUrls[position].imageUrl
        holder.setVisible(R.id.progress_bar,true)
        holder.getView<ImageView>(R.id.image_view).setOnClickListener {
            if (mUrls[position].deadlineTime!= null){
                return@setOnClickListener
            }
            listener.callback(position)
        }

        holder.setVisible(R.id.bottom_line_bg,mUrls[position].sendToUserFlag || mUrls[position].type==2)
        holder.setVisible(R.id.video_times,mUrls[position].type==2)
        holder.setVisible(R.id.countdown_container,mUrls[position].deadlineTime!= null)


        if (mUrls[position].type==2) {
            val drawable = ContextCompat.getDrawable(context, R.drawable.icon_sight_duration)
            val drawableWidth = 10
            val drawableHeight = 10
            drawable!!.setBounds(0, 0, drawableWidth, drawableHeight)
            holder.getView<TextView>(R.id.video_times).setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
            holder.getView<TextView>(R.id.video_times).text = mUrls[position].videoLength?.let {
                DateUtils.timeConversion(it)
            }
        }
        if (mUrls[position].deadlineTime!= null){
//        if (mUrls[position].sendToUser && mUrls[position].deadlineTime!= null){
            holder.setText(R.id.item_photo_status,if (mUrls[position].unlocked)"Unlocked" else "Sent" )
            val viewHolder = holder as RecallEditViewHolder
            viewHolder.messageId = mUrls[position].imageCode
            mUrls[position].deadlineTime?.let {
                RecallSendManager.getInstance()
                    .startCountDown(
                        mUrls[position],  //1688543257702
                        it*1000,
                        RecallEditCountDownListener(viewHolder)
                    )
            }
        }
        Glide.with(context)
            .load(url)
            .error(R.drawable.rc_send_thumb_image_broken)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    holder.setVisible(R.id.progress_bar,false)
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    holder.setVisible(R.id.progress_bar,false)
                    return false
                }

            })
            .into(holder.getView(R.id.image_view))


    }

    override fun getItemCount(): Int {
        return mUrls.size
    }


    private class RecallEditViewHolder(context: Context?, itemView: View?) :
        ViewHolder(context, itemView) {
        var messageId: String? = null
    }

    private class RecallEditCountDownListener(holder: RecallEditViewHolder) :
        RecallSendCountDownCallBack {
        private val mHolder: WeakReference<RecallEditViewHolder>

        init {
            mHolder = WeakReference(holder)
        }

        override fun onFinish(messageId: String, mMessage: ImagesBean) {
            val viewHolder = mHolder.get()
            if (viewHolder != null && messageId == viewHolder.messageId) {
                viewHolder.setVisible(R.id.countdown_container,false)
                mMessage.deadlineTime=null
                mMessage.sendToUserFlag=true
                mMessage.unlocked=false
            }
        }

        override fun onTick(untilFinished: Long, messageId: String) {
            val viewHolder = mHolder.get()
            if (viewHolder != null && messageId == viewHolder.messageId) {
                (viewHolder.convertView.findViewById<View>(R.id.item_photo_countdown) as TextView).text =
                    DateUtils.timeConversionHours(untilFinished)
            }
        }
    }

    interface OnCallBack {
        fun callback(position: Int)
    }
}